/**
 * 
 */
package org.softTank.BC.manager.util;

import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.log4j.Logger;
import org.softTank.BC.rs.bean.TypeBean;

/**
 * @author Yuzone
 *
 */
public class CategoryParser {

	private File sourceFile;
	protected Logger logger;

	public CategoryParser() {

	}

	public CategoryParser(File source) {
		sourceFile = source;
	}

	private DataSourceBean parser() {
		if (sourceFile == null || !sourceFile.exists()) {
			// logger.warn("Lost categoryFile!");
			TypeBean tb = new TypeBean();
			tb.setName("no category data");
			DataSourceBean dsb = new DataSourceBean();
			dsb.getType().add(tb);

			return dsb;
		}

		try {
			JAXBContext context = JAXBContext
					.newInstance("org.softTank.BC.manager.util");
			Unmarshaller unmarshaller = context.createUnmarshaller();
			return (DataSourceBean) unmarshaller.unmarshal(sourceFile);
		} catch (JAXBException e) {
			e.printStackTrace();
			return null;
		}
	}

	public List<TypeBean> getTypes() {
		return parser().getType();
	}

	public File getSourceFile() {
		return sourceFile;
	}

	public void setSourceFile(File sourceFile) {
		this.sourceFile = sourceFile;
	}
}
